package org.chen.controller;

import lombok.RequiredArgsConstructor;
import org.chen.common.domain.R;
import org.chen.domain.entity.AvatarSyncRecord;
import org.chen.domain.entity.UserVideoConfig;
import org.chen.service.AvatarService;
import org.chen.service.impl.KeyAvatarService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/avatar")
@RequiredArgsConstructor
public class KeyAvatarController {
    private final KeyAvatarService avatarService;

    @GetMapping("/sync")
    public R<Map<String, Object>> syncAvatars() {
        String result = avatarService.syncAvatarsFromHeygen();
        AvatarSyncRecord lastSync = avatarService.getLastSyncInfo();

        Map<String, Object> response = new HashMap<>();
        response.put("message", result);
        response.put("added", lastSync.getAddedCount());
        response.put("updated", lastSync.getUpdatedCount());
        response.put("failed", lastSync.getFailedCount());
        response.put("syncTime", lastSync.getSyncTime());
        return R.success(response);
    }

    @GetMapping("/config/list")
    public R<List<UserVideoConfig>> listConfigs() {
        return R.success(avatarService.listConfigs());
    }

    @PostMapping("/config/save")
    public R<String> saveConfig(@RequestBody UserVideoConfig config) {
        avatarService.saveConfig(config);
        return R.success();
    }

    @GetMapping("/sync/info")
    public R<AvatarSyncRecord> getLastSyncInfo() {
        return R.success(avatarService.getLastSyncInfo());
    }
}
